from .base_page import BasePage
from selenium.webdriver.common.by import By

class HWMoniterPage(BasePage):
    def __init__(self, webdriver):
        super().__init__(webdriver)

    def find_cpu(self, id_='hM_tab_cpu'):
        return self.find_element('id', id_)

    def find_memory(self, id_='hM_tab_mem'):
        return self.find_element('id', id_)
        
    def find_fan(self, id_='hM_tab_fans'):
        return self.find_element('id', id_)
        
    def find_psu(self, id_='hM_tab_psu'):
        return self.find_element('id', id_)
    
    def click(self, element):
        self.click_element(element)

    def find_all_elements(self, element, by=By.XPATH, str="./*"):
        return element.find_elements(by, str)
    
    """使用该函数前需要先find_all_elements获取所有部件数量，传入hw"""
    def export_all_data(self, id_xpath, hw):
        # 部件标题以及所有信息的总元素
        if '/' not in id_xpath:
            hw_tag = self.find_element('xpath', f'//*[@id="{id_xpath}"]')
        else:
            hw_tag = self.find_element("xpath", id_xpath)
        hw_infos = hw_tag.find_elements(By.CSS_SELECTOR, '[class*="ng-binding"]')
        
        len_hw_tags = int(len(hw_infos) / (len(hw)+1))
        hw_infos_list = []
        for element in hw_infos:
            hw_infos_list.append(element.text)
        hw_tags = hw_infos_list[:len_hw_tags]
        # 创建N个hw对应的字典
        hw_lists = [{} for _ in range(len(hw))]
        for i in range(len(hw)):
            for index, key in enumerate(hw_tags):
                hw_lists[i][key] = hw_infos_list[(i+1)*len(hw_tags)+index]
        return hw_tags, hw_lists
        
        